public class Leetcode235 {
    TreeNode res;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        dfs(root,p,q);
        return res;
    }
    public void dfs(TreeNode root,TreeNode p,TreeNode q){
        if (root==null){
            return;
        }
        if ((root.val- p.val)*(root.val-q.val)<=0){
            res = root;
        }else if (root.val>p.val&&root.val>q.val){
            dfs(root.left,p,q);
        }else {
            dfs(root.right,p,q);
        }
    }
}
